//
//  SwlftSQLViewController.swift
//  SQLiteDemo
//
//  Created by lete on 2020/6/9.
//  Copyright © 2020 lete. All rights reserved.
//

import UIKit

class SwlftSQLViewController: UIViewController {
    // 数据库声明
//           var database: Database!
     var dbManager: DatabaseManager!
   
    override func viewDidLoad() {
        super.viewDidLoad()
        
                  
                  let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
                  debugPrint("数据库地址:===================")
                  debugPrint(paths)
        
        
//                  // 与数据库建立连接
//                  database = Database()
//
//                  // 建立列表（有列表后不再建立）
//                  database.tableLampCreate()
//
//                  // 插入两条数据
//                  database.tableLampInsertItem(address: 51, name: "灯光1", colorValue: "#FFFFFF", lampType: 0)
//                  database.tableLampInsertItem(address: 52, name: "灯光2", colorValue: "#AAAAAA", lampType: 1)
//
//                  // 遍历列表（检查插入结果）
//                  database.queryTableLamp()
//
//                  // 根据条件查询
//                  database.readTableLampItem(address: 52)
//
//                  // 修改列表项
//                  database.tableLampUpdateItem(address: 51, newName: "客厅大灯")
//
//                  // 遍历列表（检查修改结果）
//                  database.queryTableLamp()
//
//                  // 删除列表项
//                  database.tableLampDeleteItem(address: 52)
//
//                  // 遍历列表（检查删除结果）
//                  database.queryTableLamp()

        

                        // 与数据库建立连接
                        dbManager = DatabaseManager()
    
      //                  database.tableLampInsertItem(address: 51, name: "灯光1", colorValue: "#FFFFFF", lampType: 0)
      //                  database.tableLampInsertItem(address: 52, name: "灯光2", colorValue: "#AAAAAA", lampType: 1)
      //
        
         // 建立列表（有列表后不再建立）
                          dbManager.tableTestCreate()
        
        
    }
    
    @IBAction func add(_ sender: Any) {
        
        for (index, item) in ["刘订单","得到","维护费"].enumerated(){
               let testModel =  DBTestModel()
            testModel.name = item
            testModel.age = Int64(index + 20)
            
            dbManager.tableTestInsertItem(model: testModel)
        }
        
    }
    
    @IBAction func dele(_ sender: Any) {
        dbManager.tableTestDeleteItemWithCondition(name: "维护费")
    }
    
    @IBAction func updata(_ sender: Any) {
        
        dbManager.tableTestUpdateItemWithCondition(name: "得到", newName: "tiantin")
        
    }
    
    @IBAction func findWithCondition(_ sender: Any) {
       let re =  dbManager.readTableTestItemFindWithCondition(name: "刘订单")
//        let model:DBTestModel = (re as? DBTestModel)!
        
        debugPrint(re)
        
    }
    
    @IBAction func findAll(_ sender: Any) {
      let reArr =  dbManager.queryTableTestFindAll()
        
        debugPrint(reArr)
    }
    
    @IBAction func deleTable(_ sender: Any) {
        dbManager.tableTestDelete()
    }
    
    @IBAction func deleDB(_ sender: Any) {
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
